// pages/recommend/recommend.js
var api = require('../../../app/app.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nothing: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/no.jpg',
    showPage: true,
    showInfo: false,
    bott: 'https://www.sdhjfu.com/smallhouse/public/tianxing/images/bottom.jpg',
    array: [],
    index: 0,
    colorStyle: 'color: #333',
    objectName: '',
    recommendName: '',
    showMask: true,
    items: [
      { name: '1', value: '男', checked: true},
      { name: '2', value: '女'}
    ],
    customerName: '',
    customerGender: '',
    customerTel: '',
    bac_tip: 'display: none',
    form_info: '',
    userId: '',
    phone: '',
    result_ind: '',
    result_name: '',
    ind: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '推荐客户'
    })
    var that = this
    var houseid = wx.getStorageSync('houseid') // 同步获取存储houseid
    wx.request({// 获取信息
      url: api.Index,
      method: 'POST', //请求方式
      data: {
        houseid: houseid
      }, //请求参数格式为json
      header: {
        'content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        var result_ind = []
        var result_name = []
        for (let i in res.data.adviserlist) {  
          result_ind.push(res.data.adviserlist[i].adviid) 
          result_name.push(res.data.adviserlist[i].adviname) 
        }
        that.setData({
          consultantInfo: res.data,
          result_ind: result_ind,
          result_name: result_name,
          objectName: res.data.houre.house_name
        })
      }
    })
    
  },
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      ind: this.data.result_ind[e.detail.value],
    })
  },
  sumbitClick: function(e) { // 提交表单
    if (!(e.detail.value.customername && e.detail.value.gender && e.detail.value.tel)) {
      wx.showModal({
        title: '提示',
        content: '请填写对应客户信息，*为必填',
        success(res) {
          if (res.confirm) {
            //console.log('用户点击确定')
          } else if (res.cancel) {
            //console.log('用户点击取消')
          }
        }
      })
    } else {
      if (!(/^1[3456789]\d{9}$/.test(e.detail.value.tel))) {
        wx.showToast({
          title: '手机号码有误',
          duration: 1500,
          icon: 'none'
        });
        return false;
      } else {
        var that = this;
        var houseid = wx.getStorageSync('houseid')
        var userInfo = { // 获取用户信息
          customername: e.detail.value.customername,
          gender: e.detail.value.gender,
          tel: e.detail.value.tel,
          remarks: e.detail.value.remarks,
          userId: that.data.userId,
          mortId: that.data.ind,
          houseId: houseid
        }
        // console.log(userInfo)
        wx.request({ // 传数据给后台
          url: api.Recommend,
          method: 'POST',
          data: userInfo,
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            // console.log(res.data)
            if (res.data.re == 'succ') {
              that.setData({ // 清空表单
                form_info: ''
              })
              wx.navigateTo({ // 跳转到成功页面
                url: '../successTips/successTips',
              })
            } else if (res.data.re == 'diff'){
              that.setData({ // 已经被推荐过
                bac_tip: 'display: block',
                showMask: false
              })
            }
          }
        })
      }
    }
  },
  maskClick: function() {
    this.setData({
      bac_tip: 'display: none',
      showMask: true
    })
  },
  butLogin: function() {
    wx.showModal({
      title: '您还未登陆',
      content: '请先登录再进行操作',
      success(res) {
        if (res.confirm) {
          //console.log('用户点击确定')
          wx.redirectTo({
            url: '/pages/authorization/authorization'
          })
        } else if (res.cancel) {
          //console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({ // 获取本地缓存
      key: 'userReInfo',
      success(res) {
        // console.log(res.data)
        var nickName = res.data.nickName
        var userId = res.data.userId
        var phone = res.data.phone
        that.setData({
          recommendName: nickName,
          userId: userId,
          phone: phone
        })
        if (!that.data.phone) {
          that.setData({
            showPage: true
          })
        } else {
          that.setData({
            showPage: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})
